﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace numer
{
    public partial class NumberInput : Form
    {
        #region 定数
        //各画面に割り振られている番号
        private const int MAINSCREEN = 1;
        private const int INPUTSCREEN = 2;
        private const int PLAYSCREEN = 3;
        private const int RESULTSCREEN = 4;

        private const string DaleteNonQuery = @"DB.SQL.Delete_Select_Table.sql";
        #endregion

        #region 構造体

            //入力値を受け渡すための構造体を追加 2015/1/17 M.Fukunishi
        //public int[] parsonal = new int[3] { 0, 0, 0 };

        PlayGameScreen playGameScreen = null;

        //public int[] Parsonal
        //{
        //    get { return parsonal; }
        //    set { parsonal = value; }
        //}


        #endregion

        /// <summary>
        /// コンストラクタ
        /// </summary>
        public NumberInput()
        {
            InitializeComponent();
        }



        /// <summary>
        /// 画面ロード
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void NumberInputScreen_Load(object sender, EventArgs e)
        {

        }


        /// <summary>
        /// 0ボタン押下
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn0_Click(object sender, EventArgs e)
        {
            //画面出力
            OutPanel(this.btn0.Text);
        }


        /// <summary>
        /// 1ボタン押下
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn1_Click(object sender, EventArgs e)
        {
            //画面出力
            OutPanel(this.btn1.Text);
        }


        /// <summary>
        /// 2ボタン押下
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn2_Click(object sender, EventArgs e)
        {
            //画面出力
            OutPanel(this.btn2.Text);
        }


        /// <summary>
        ///  3ボタン押下
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn3_Click(object sender, EventArgs e)
        {
            //画面出力
            OutPanel(this.btn3.Text);
        }


        /// <summary>
        /// 4ボタン押下
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn4_Click(object sender, EventArgs e)
        {
            //画面出力
            OutPanel(this.btn4.Text);
        }


        /// <summary>
        /// 5ボタン押下
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn5_Click(object sender, EventArgs e)
        {
            //画面出力
            OutPanel(this.btn5.Text);
        }


        /// <summary>
        /// 6ボタン押下
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn6_Click(object sender, EventArgs e)
        {
            //画面出力
            OutPanel(this.btn6.Text);
        }


        /// <summary>
        /// 7ボタン押下
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn7_Click(object sender, EventArgs e)
        {
            //画面出力
            OutPanel(this.btn7.Text);
        }


        /// <summary>
        /// 8ボタン押下
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn8_Click(object sender, EventArgs e)
        {
            //画面出力
            OutPanel(this.btn8.Text);
        }


        /// <summary>
        /// 9ボタン押下
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn9_Click(object sender, EventArgs e)
        {
            //画面出力
            OutPanel(this.btn9.Text);
        }


        /// <summary>
        /// 決定ボタン押下
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnEnter_Click(object sender, EventArgs e)
        {

            bool startFlg = true;

            //ボタン非活性化
            BtnActivate();

            //スレッドを生成する M.Hamada 2015/03/15
            Thread thread = new Thread(new ThreadStart(ThreadStart));
            thread.Start();

            //スレッドの終了
            thread.Join();


            //時間内にテーブルレコード数が2にならなかった場合
            if (!startFlg)
            {
                //確立失敗メッセージ
                MessageBox.Show("タイムアウトになりました。\n スタート画面へ戻ります。");

                //初めの画面へ戻る
                //画面遷移
                Program.FormContainer.Change(MAINSCREEN);
        
                //TODO:もしテーブルレコード数が0でなければテーブルの初期化を行う
                //if()
                //{
                //    DB.DBLogic.ExecuteNonQuery(DaleteNonQuery);
                //}

                //処理の終了
                return;
            }
            
            //構造体personalに入力値を格納 2015/1/17 M.Fukunishi
            int pnum1 = 0;
            int.TryParse(lblOutPanel1.Text, out pnum1);
            int pnum2 = 0;
            int.TryParse(lblOutPanel2.Text, out pnum2);
            int pnum3 = 0;
            int.TryParse(lblOutPanel3.Text, out pnum3);

            //Program.FormContainer.mPlayGameScreen.parsonal[0] = pnum1;
            //Program.FormContainer.mPlayGameScreen.parsonal[1] = pnum2;
            //Program.FormContainer.mPlayGameScreen.parsonal[2] = pnum3;


            //parsonal[0] = pnum1;
            //parsonal[1] = pnum2;
            //parsonal[2] = pnum3;

            //次の画面のプロパティに入力値を引き継ぐ
            Program.FormContainer.mPlayGameScreen.parsonal = pnum1.ToString() + " " + pnum2.ToString() + " " + pnum3.ToString();

            //  入力した数値を初期化
            this.lblOutPanel1.Text = "";
            this.lblOutPanel2.Text = "";
            this.lblOutPanel3.Text = "";
            //　決定ボタンを非表示
            btnEnter.Enabled = false;

            //画面遷移
            Program.FormContainer.Change(PLAYSCREEN);
        }

        /// <summary>
        /// 選択パネルに値表示
        /// </summary>
        private void OutPanel(string number)
        {

            //入力値がパネル①またはパネル②と同じの場合
            if (number == this.lblOutPanel1.Text ||
                    number == this.lblOutPanel2.Text)
            {
                //処理終了
                return;
            }

         
            //パネル①が空白の場合
            if(string.IsNullOrEmpty(this.lblOutPanel1.Text))
            {
                //選択値をパネル①に出力する
                this.lblOutPanel1.Text = number;
            }
            //パネル①に入力値があり、パネル②が空白の場合
            else if(string.IsNullOrEmpty(this.lblOutPanel2.Text))
            {
                //選択値をパネル②に出力する
                this.lblOutPanel2.Text = number;
            }
            //パネル①およびパネル②に値が入っているかつパネル③の場合
            else if(string.IsNullOrEmpty(this.lblOutPanel3.Text))
            {
                //選択値をパネル③に出力する
                this.lblOutPanel3.Text = number;
                //決定ボタンを表示
                btnEnter.Enabled = true;
            }
            //パネルに3つ数字が入っているときは入力しない
            else
            {
            
            }

        }


        /// <summary>
        /// 削除ボタン
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnClear_Click(object sender, EventArgs e)
        {

            //パネル③に値が入っていれば
            if (!string.IsNullOrEmpty(this.lblOutPanel3.Text))
            {
                //選択値をパネル③の値を消す
                this.lblOutPanel3.Text = "";

                // 決定ボタンを非表示
                btnEnter.Enabled = false;
            }
            else if (!string.IsNullOrEmpty(this.lblOutPanel2.Text))
            {
                //選択値をパネル②の値を消す
                this.lblOutPanel2.Text = "";
            }
            else
            {
                //選択値をパネル①の値を消す
                this.lblOutPanel1.Text = "";
            }

        }


        /// <summary>
        /// スレッド処理
        /// </summary>
        private void ThreadStart()
        {

            System.Threading.Thread.Sleep(3000);

        }


        /// <summary>
        /// ボタン非活性処理
        /// </summary>
        public void BtnActivate()
        {

            if (this.btnEnter.Enabled)
            {
                this.btn0.Enabled = false;
                this.btn1.Enabled = false;
                this.btn2.Enabled = false;
                this.btn3.Enabled = false;
                this.btn4.Enabled = false;
                this.btn5.Enabled = false;
                this.btn6.Enabled = false;
                this.btn7.Enabled = false;
                this.btn8.Enabled = false;
                this.btn9.Enabled = false;
                this.btnClear.Enabled = false;
                this.btnEnter.Enabled = false;
            }
            else
            {

                this.btn0.Enabled = true;
                this.btn1.Enabled = true;
                this.btn2.Enabled = true;
                this.btn3.Enabled = true;
                this.btn4.Enabled = true;
                this.btn5.Enabled = true;
                this.btn6.Enabled = true;
                this.btn7.Enabled = true;
                this.btn8.Enabled = true;
                this.btn9.Enabled = true;
                this.btnClear.Enabled = true;

            }

        }

    }
}
